package com.ziq.baselib.test.horizontalpage

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.ziq.base.base.BaseActivity
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import com.ziq.base.view.recycleview.BaseMultiViewHolder
import com.ziq.baselib.R
import com.ziq.baselib.databinding.ActivityHorizontalPageBinding
import dagger.hilt.android.AndroidEntryPoint
import java.io.*


@AndroidEntryPoint
class HorizontalPageActivity : BaseActivity() {

    private val binding : ActivityHorizontalPageBinding by binding()

    val adapter = MyAdapter()

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.root
        binding.recyclerView.layoutManager = GridLayoutManager(this, 4, RecyclerView.HORIZONTAL, false)
        binding.recyclerView.adapter = adapter

        val snapHelper = PagerSnapHelper()
        snapHelper.attachToRecyclerView(binding.recyclerView)

        binding.btnUpdate.click {
            val list = mutableListOf<String>()
            for (index in 0..13){
                list.add("a$index")
            }
            adapter.setData(list)
        }
    }

    class MyAdapter : RecyclerView.Adapter<BaseMultiViewHolder>(){

        val dataList = mutableListOf<String>()

        fun setData(list: MutableList<String>){
            dataList.clear()
            dataList.addAll(list)
            this.notifyDataSetChanged()
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseMultiViewHolder {
            val view: View = LayoutInflater.from(parent.context).inflate(R.layout.item_text, parent, false)
            return BaseMultiViewHolder(view)
        }

        override fun onBindViewHolder(holder: BaseMultiViewHolder, position: Int) {
            val item = dataList.get(position)
            holder.getViewById<TextView>(R.id.tv).text = "$position : $item"
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

    }
}